package com.dhcc.crftool.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhcc.crftool.entity.CRFMetaData;
import com.dhcc.crftool.enums.ResultStatusEnum;
import com.dhcc.crftool.service.CRFMetaDataService;
import com.dhcc.crftool.uilts.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Api(value = "CRF接口", tags="CRF管理metadata")
@RestController
@RequestMapping("/crf/metadata")
public class CRFManageMetaDateController {
    @Resource
    private CRFMetaDataService crfMetaDataService;

    @ApiOperation(value = "查询所有数据", notes = "查询所有数据", httpMethod = "GET")
    @GetMapping("/getAll")
    public ResultModel getAll(){
        return ResultModel.ok(crfMetaDataService.list());
    }

    @ApiOperation(value = "根据条件分页查询所有数据", notes = "根据条件分页查询所有数据", httpMethod = "GET")
    @PostMapping("/getByPageByItem")
    public ResultModel getByPageByItem( int page,int limit,CRFMetaData crfMetaData){
        System.out.println(page);
        System.out.println(limit);System.out.println(crfMetaData);
        IPage<CRFMetaData> page1 = crfMetaDataService.page(new Page<>(1L, 3L), null);
        List<CRFMetaData> records = page1.getRecords();
        Iterator<CRFMetaData> iterator = records.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
//        总结：不知道什么原因失效,baseMaper，也失效了。
        crfMetaDataService.getByPageByItem();
        return ResultModel.ok();

    }

    @ApiOperation(value="根据条件来查询数据",notes = "根据条件来查询数据",httpMethod = "GET")
    @GetMapping("/getById")
    public ResultModel getById(int id){
        return ResultModel.ok(crfMetaDataService.getById(id),"根据id来查询数据成功");
    }
    @ApiOperation(value="根据条件来查询数据",notes = "根据条件来查询数据",httpMethod = "GET")
    @GetMapping("/getByFormId")
    public ResultModel getByFormId(@RequestParam String formId){
        QueryWrapper<CRFMetaData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_id",formId);
        CRFMetaData one = crfMetaDataService.getOne(queryWrapper);
        return ResultModel.ok(one,"根据formId来查询数据成功");
    }

    @ApiOperation(value = "根据对象添加数据", notes = "根据对象添加数据", httpMethod = "POST")
    @PostMapping("/insert")
    public ResultModel insert(@RequestBody CRFMetaData crfMetaData){
        crfMetaDataService.save(crfMetaData);
        return ResultModel.ok(null, ResultStatusEnum.CREATED.getCode());
    }



    @ApiOperation(value = "根据id删除数", notes = "根据id删除数", httpMethod = "GET")
    @GetMapping("/delete")
    public ResultModel delete(int id){
        crfMetaDataService.removeById(id);
        return ResultModel.ok(null,ResultStatusEnum.NO_CONTENT.getCode());
    }

    @ApiOperation(value = "根据ids批量删除数", notes = "根据id批量删除数", httpMethod = "GET")
    @GetMapping("/deleteBatchIds")
    public ResultModel deleteBatchIds(int[] idList){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < idList.length; i++) {
            list.add(idList[i]);
        }
        crfMetaDataService.removeByIds(list);
        return ResultModel.ok();
    }

    @ApiOperation(value = "根据id修改数据", notes = "根据id修改数据", httpMethod = "POST")
    @PostMapping("/update")
    public ResultModel update(@RequestBody CRFMetaData crfMetaData){
        int id = crfMetaData.getId();
        UpdateWrapper<CRFMetaData> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        crfMetaDataService.update(crfMetaData,updateWrapper);
        return ResultModel.ok();
    }
}
